package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.basic.StringUtil;
import com.qire.manhua.dal.IComicDataDAL;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.ComicQuickReadViewModel;
import com.qire.manhua.model.vo.CollectTaskVO;
import com.qire.manhua.model.vo.ComicQuickReadInfoVO;
import com.qire.manhua.model.vo.ComicQuickReadInfoVO.QuickReadDetailsInfoVO;

public class ComicQuickReadService extends DataService {

    private final IComicDataDAL comicDataDAL = WarehouseFactory.create(this, IComicDataDAL.class);

    private final ComicQuickReadViewModel viewModel;

    public ComicQuickReadService(ComicQuickReadViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 获取漫画快速阅读相关信息
     */
    public void fetchComicQuickReadInfo(int comicId) {
        viewModel.showLoadDialog();
        comicDataDAL.fetchComicQuickReadInfo(comicId);
    }
    @StrategyAnnotation(event = {Url.ComicQuickReadData})
    public void fetchComicQuickReadInfoCallBack(ComicQuickReadInfoVO dataVO, boolean isCache) {
        viewModel.dismissLoadDialog();
        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        final ComicBookEntity comicBookEntity = viewModel.comicBookEntity();

        if(ObjectUtil.nonNull(dataVO.chapterInfo)) {
            dataVO.chapterInfo.fillComic(comicBookEntity);
            viewModel.setNextChaptersId(dataVO.chapterInfo.next_id);
            viewModel.refreshChapterInfo(dataVO.chapterInfo.chapter_name);
        }

        if (ObjectUtil.nonNull(dataVO.readChapter)) {
            viewModel.loadHistoryInfo(dataVO.readChapter.id, dataVO.readChapter.chapter_px);
        }

        if(ObjectUtil.nonNull(dataVO.bookInfo)) {
            QuickReadDetailsInfoVO infoVO = dataVO.bookInfo;
            comicBookEntity.whetherCollect(infoVO.shelf == 1);
            viewModel.proxy().loadDetailsData(infoVO.book_name, infoVO.getTagText(), infoVO.book_intro,
                    "已更新至" + infoVO.chapter_px + "话", StringUtil.toSymbol(infoVO.tj.like_cnt));
        }
    }

    /**
     * 切换收藏状态通过漫画ID
     * @param comicId 漫画ID
     */
    public void toggleCollectionBy(int comicId) {
        comicDataDAL.whetherJoinMyBookshelf(comicId, 0);
    }
    @StrategyAnnotation(event = Url.whetherJoinMyBookshelf)
    public void whetherJoinMyBookshelfCallback(CollectTaskVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO) || dataVO.isFail()) {
            viewModel.refreshFavorite("收藏失败", "");
            return;
        }

        String resultMsg = dataVO.getResultMsg();
        String takeMsg = dataVO.getTaskMsg();
        viewModel.refreshFavorite(resultMsg, takeMsg);
    }

}
